<?php

class UserModel extends Model
{
    public function getinfo($userid)
    {
        $info = $this->where(array('id' => $userid))->find();
        if ($info) {
            return $this->dataAppend($info);
        }
        return false;
    }
	public function getinfoByname($username)
    {
        $info = $this->where(array('username' => $username))->find();
        if ($info) {
            return $this->dataAppend($info);
        }
        return false;
    }

    public function dataAppend($info)
    {
        $info['point'] = 20 * $info['spend'] + 10 * $info['extcredits1'] + $info['extcredits2'];
        if (is_file(PT_ROOT . '/public/avatar/' . subid(subid($info['id'])) . '/' . subid($info['id']) . '/' . $info['id'] . '/big.jpg')) {
            $info['avatar_big'] = PT_DIR . '/public/avatar/' . subid(subid($info['id'])) . '/' . subid($info['id']) . '/' . $info['id'] . '/big.jpg';
            $info['avatar_middle'] = PT_DIR . '/public/avatar/' . subid(subid($info['id'])) . '/' . subid($info['id']) . '/' . $info['id'] . '/middle.jpg';
            $info['avatar_small'] = PT_DIR . '/public/avatar/' . subid(subid($info['id'])) . '/' . subid($info['id']) . '/' . $info['id'] . '/small.jpg';
        } elseif (is_file(PT_ROOT . '/public/avatar/' . subid(subid($info['id'])) . '/' . subid($info['id']) . '/' . $info['id'] . '/big.gif')) {
            $info['avatar_big'] = PT_DIR . '/public/avatar/' . subid(subid($info['id'])) . '/' . subid($info['id']) . '/' . $info['id'] . '/big.gif';
            $info['avatar_middle'] = PT_DIR . '/public/avatar/' . subid(subid($info['id'])) . '/' . subid($info['id']) . '/' . $info['id'] . '/middle.gif';
            $info['avatar_small'] = PT_DIR . '/public/avatar/' . subid(subid($info['id'])) . '/' . subid($info['id']) . '/' . $info['id'] . '/small.gif';
        } else {
            $info['avatar_small'] = PT_DIR . '/public/image/noavatar_small.gif';
            $info['avatar_middle'] = PT_DIR . '/public/image/noavatar_middle.gif';
            $info['avatar_big'] = PT_DIR . '/public/image/noavatar_big.gif';
        }
        $info['usergroup'] = M('usertype')->where(array('ext' => array('<', $info['point']), 'type' => 'user'))->order('ext desc')->getfield('showname');
        $info['vipgroup'] = '';
        if ($info['viptype'] > 0) {
            $vipinfo = M('usertype')->field('showname,id')->where(array('ext' => array('<', $info['spend']), 'type' => 'vip'))->order('ext desc')->find();
            if ($vipinfo) {
                if ($vipinfo['id'] > $info['viptype']) {
                    M('user')->where(array('id' => $info['id']))->update(array('viptype' => $vipinfo['id']));
                }
            } else {
                $vipinfo = M('usertype')->field('showname,id')->where(array('id' => $info['viptype']))->find();
            }
            $info['vipgroup'] = $vipinfo['showname'];
        }
        $info['url']='';
        return $info;
    }

    /**
     * 登录
     *
     * @param $where
     * @param $password
     * @return bool
     */
    public function login($where, $password)
    {
        $info = $this->field('id,email,password,salt,username')->where($where)->find();
        if ($info && $info['password'] == md5(md5($password) . $info['salt'])) {
            // 更新登录信息
            $this->where(array('id' => $info['id']))->update(array('loginip' => ip2long($_SERVER['REMOTE_ADDR']), 'logindate' => NOW_TIME));
            // 刷新用户缓存
            dc::user($info['id'], true);
            // 写入cookie
            $this->setLoginStatus($info);
            return true;
        } else {
            return false;
        }
    }

    /**
     * 用户注册
     *
     * @param array $param
     * @return bool
     */
    public function reg(array $param)
    {
        $param['regip'] = $param['loginip'] = ip2long($_SERVER['REMOTE_ADDR']);
        $param['regdate'] = $param['logindate'] = NOW_TIME;
        $param['salt'] = substr(md5(NOW_TIME), 0, 6);
        $param['password'] = md5($param['password'] . $param['salt']);
        $param['extcredits1'] = C('USER_REG_extcredits1', null, 0); //贡献
        $param['extcredits2'] = C('USER_REG_extcredits2', null, 10); //铜币
        $param['extcredits3'] = C('USER_REG_extcredits3', null, 0); //银币
        $param['extcredits4'] = C('USER_REG_extcredits4', null, 0); //金币
        $param['status'] = C('USER_REG_STATUS', null, 1);
        $param['intro'] = C('USER_REG_INTRO', null, '这家伙很懒，什么都没有留下');
        if ($param['id'] = $this->insert($param)) {
            $this->setLoginStatus($param);
            return true;
        } else {
            echo $this->getError();
            return false;
        }
    }

    public function setLoginStatus($info)
    {
        cookie('userid', $info['id']);
        cookie('username', $info['username']);
        cookie('token', md5($info['username'] . $info['password'] . NOW_TIME));
    }

    public function delLoginStatus()
    {
        cookie('userid', null);
        cookie('token', null);
        setcookie('syncuyan', '', time() + 3600, '/', '');
    }

    /**
     * 校验登录状态
     *
     * @param bool $force true进行token校验
     * @return bool
     */
    public function checklogin($force = false)
    {
        $userid = intval(cookie('userid'));
        if ($userid) {
            //todo close
            $info = dc::user($userid, true);
            if ($force) {
                if (cookie('token') !== md5($info['username'] . $info['password'] . $info['logindate'])) {
                    return false;
                }
            }
            return $info;
        }
        return false;
    }

    //检测openid是否已经注册
    public function checkOpenid($openid, $thirdtype)
    {
        $thirdtype .= 'id';
        if ($info = M('user')->field('id,username,email,password')->where(array($thirdtype => $openid))->find()) {
            return $info;
        }
        return false;
    }
}